/****************************************************
    Author:            龙之介
    CreatTime:    2021/2/11 12:51:14
    Description:     Nothing
*****************************************************/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using System.Linq;
using DG.Tweening;
using ZYKTool;

namespace LongZhiJie
{
	public class PlayerNew : MonoBehaviour
	{

        public float speed;
        public Stand _Playerstand;


        private bool CanShowChangeGravaty = true;
        private Rigidbody2D rb;
        private Vector2 moveVelocity;
        private bool canY = false;
        private bool canX = true;
        public Collision2D Col_Vertical;
        public Collision2D Col_Herizatal;
        private bool m_hitDelect;
        private RaycastHit2D[] m_HitS;
        public  Vector2 _origin;
        private  Vector2 _size=new Vector2(80,40);
        private void Start()
        {
            //transform.GetOrAddComponent<ZYKTool.Timer.ZYKTimerSystemTool>();
            rb = GetComponent<Rigidbody2D>();
        }

        private void Update()
        {
        //    if (TapTapStaticValue.PlayerAttributes.Is禁锢) return;
            float x = 0;
            float y = 0;
            ///控制人物移动

                if (canX)
                {
                    x = Input.GetAxisRaw("Horizontal");
                }
                if (x == 0 && canY)
                {
                    y = Input.GetAxisRaw("Vertical");
                }

            moveVelocity = new Vector2(x, y) * speed;//TapTapStaticValue.PlayerAttributes.Speed;
            if (Input.GetMouseButton(0))
            {
                Vector3 target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                moveVelocity += (Vector2)(-1 * (target - transform.position).normalized) * 2;

                if (_Playerstand == Stand.down || _Playerstand == Stand.up)
                {
                    moveVelocity -= new Vector2(0, moveVelocity.y);
                }
                else
                {
                    moveVelocity -= new Vector2(moveVelocity.x, 0);
                }
                //moveVelocity *= TapTapStaticValue.UpdateRandom后座力;
            }


            
            // Debug.LogError("??");
            //控制切换重力技能
            if (Input.GetKeyDown(KeyCode.Q) && CanShowChangeGravaty)
            {
                Debug.LogError(ZYKTool.Timer.ZYKTimerSystemTool.Instance);
                CanShowChangeGravaty = false;
                ZYKTool.Timer.ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                    (() => { CanShowChangeGravaty = true; }, 1, 1, ZYKTool.Timer.PETimeUnit.Second);
                改变重力();
            }
            
        }
        private void FixedUpdate()
        {
            if(InitManger.Instance!=null)
            {
                if(!InitManger.Instance.CanMove)return;
            }
            rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);
        }
        private void OnDrawGizmos()
        {
            Gizmos.color = Color.red;
         Gizmos.DrawWireCube((Vector2)transform.position + _origin, _size);
            
        }
        public void 玩家当前站立位置(Stand stand)
        {
            _Playerstand = stand;
            if (CanShowChangeGravaty == false)
            {
                ZYKTool.Timer.ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                (() => { transform.DOKill(); }, 0.1f, 1, ZYKTool.Timer.PETimeUnit.Second);
            }
        }

        public void 改变重力()
        {
            if (tempStand != Stand.none) return;
            transform.DOKill();// Debug.LogError(_Playerstand);
                               //Debug.LogError(_Playerstand);
            m_HitS = Physics2D.BoxCastAll(_origin + (Vector2)transform.position, _size, 100, transform.forward);
            float disx =999999999, disy = 999999999;
            if (m_HitS.Length != 0)
            {

                foreach (RaycastHit2D ray in m_HitS)
                {
                    string dir = ray.collider.tag.ToLower();
                    if (dir == _Playerstand.ToString())
                    {
                        Debug.LogError("same");
                        continue;
                    }
                    Debug.LogError(dir);
                    switch (dir)
                    {
                        case "down": disy = Mathf.Min(disy, Mathf.Abs(transform.position.y - ray.collider.transform.position.y)); break;
                        case "up": disy = Mathf.Min(disy, Mathf.Abs(transform.position.y - ray.collider.transform.position.y)); break;
                        case "right": disx = Mathf.Min(disx, Mathf.Abs(transform.position.x - ray.collider.transform.position.x)); break;
                        case "left": disx = Mathf.Min(disx, Mathf.Abs(transform.position.x - ray.collider.transform.position.x)); break;
                    }
                }

            }
            Debug.LogError(disx + "  " + disy);

            if (TapTapStaticValue.UpdateRandom重力切换改为闪现)
            {
              


                switch (_Playerstand)
                {
                    case Stand.down:
                        transform.DOMove(new Vector3(transform.position.x, disy + transform.position.y, transform.position.z), 0.1f);
                        break;
                    case Stand.up:

                        transform.DOMove(new Vector3(transform.position.x, -disy + transform.position.y, transform.position.z), 0.1f);
                        break;
                    case Stand.left:

                        transform.DOMove(new Vector3(disx + transform.position.x, transform.position.y, transform.position.z), 0.1f);
                        break;
                    case Stand.right:
                        transform.DOMove(new Vector3(-disx + transform.position.x, transform.position.y, transform.position.z), 0.1f);
                        break;
                }
            }
            else
            {
                switch (_Playerstand)
                {
                    case Stand.down:
                        transform.DOMove(new Vector3(transform.position.x, disy + transform.position.y, transform.position.z), 0.5f);
                        break;
                    case Stand.up:

                        transform.DOMove(new Vector3(transform.position.x, -disy + transform.position.y, transform.position.z), 0.5f);
                        break;
                    case Stand.left:

                        transform.DOMove(new Vector3(disx + transform.position.x, transform.position.y, transform.position.z), 0.5f);
                        break;
                    case Stand.right:
                        transform.DOMove(new Vector3(-disx + transform.position.x, transform.position.y, transform.position.z), 0.5f);
                        break;
                }
            }

        }
        public enum Stand
        {
            none,
            up,
            down,
            right,
            left,
        }
        private Stand tempStand = Stand.none;
        private void OnTriggerEnter2D(Collider2D collision)
        {

            if (collision.tag == "Up")
            {
                if (!CanShowChangeGravaty)
                {
                    tempStand = Stand.none;
                    _Playerstand = Stand.up;
                }
                else
                {
                    //如果玩家进入到新的地方  预备更新为玩家上次的  玩家等于进入的状态
                    tempStand = _Playerstand;
                    _Playerstand = Stand.up;

                }
                canX = true;
            }
            if (collision.tag == "Left")
            {
                if (!CanShowChangeGravaty)
                {
                    tempStand = Stand.none;
                    _Playerstand = Stand.left;
                }
                else
                {
                    //如果玩家进入到新的地方  预备更新为玩家上次的  玩家等于进入的状态
                    tempStand = _Playerstand;
                    _Playerstand = Stand.left;

                }
                canY = true;
            }
            if (collision.tag == "Right")
            {
                if (!CanShowChangeGravaty)
                {
                    tempStand = Stand.none;
                    _Playerstand = Stand.right;
                }
                else
                {
                    //如果玩家进入到新的地方  预备更新为玩家上次的  玩家等于进入的状态
                    tempStand = _Playerstand;
                    _Playerstand = Stand.right;

                }
                canY = true;
            }
            if (collision.tag == "Down")
            {
                if (!CanShowChangeGravaty)
                {
                    tempStand = Stand.none;
                    _Playerstand = Stand.down;
                }
                else
                {
                    //如果玩家进入到新的地方  预备更新为玩家上次的  玩家等于进入的状态
                    tempStand = _Playerstand;
                    _Playerstand = Stand.down;

                }
                canX = true;
            }
        }
        private void OnTriggerExit2D(Collider2D collision)
        {

            if (collision.tag == "Up")
            {
                canX = false;
                if (_Playerstand == Stand.up)//玩家刚进去 然后出来 那就返回到之前temp
                {
                    _Playerstand = tempStand;
                }
                tempStand = Stand.none;
            }
            if (collision.tag == "Left")
            {
                if (_Playerstand == Stand.left)//玩家刚进去 然后出来 那就返回到之前temp
                {
                    _Playerstand = tempStand;
                }
                canY = false;
                tempStand = Stand.none;
            }
            if (collision.tag == "Right")
            {
                if (_Playerstand == Stand.right)//玩家刚进去 然后出来 那就返回到之前temp
                {
                    _Playerstand = tempStand;
                }
                canY = false;
                tempStand = Stand.none;
            }
            if (collision.tag == "Down")
            {
                if (_Playerstand == Stand.down)//玩家刚进去 然后出来 那就返回到之前temp
                {
                    _Playerstand = tempStand;
                }
                canX = false;
                tempStand = Stand.none;
            }
        }


    }
}